#include "khaibao.h"
void Nhap(int *p,int n)
{
	srand(unsigned(time(0)));
	for(int i=0;i<n;i++)
	{
		*(p+i)=rand()%100;
	}
}
void GhiFile(int *p,int n)
{
	ofstream write;
	write.open("/home/lucky/workspace/file/src/mang1chieu.txt");
	for(int i=0;i<n;i++)
	{
		write<<*(p+i)<<"\t";
	}
	write.close();
}
void DocFile(char *s)
{
	char s1;
	ifstream read;
	read.open(s);
	if(read.fail())
	{
		cout<<"could not be opened.";
		return;
	}
	while(!read.eof())
	{
		read.get(s1);
		cout<<s1;
	}
	read.close();
}
int TongMang(int n)
{
	ifstream read;
	int s=0,*p;
	p=new int [n];
	read.open("/home/lucky/workspace/file/src/mang1chieu.txt");
	int m=0;
	while(!read.eof())
	{
		read>>*(p+m);
		m++;
	}
	read.close();
	for(int i=0;i<m;i++)
	{
		s+=*(p+i);
	}
	return s;
}
void HoanVi(int &a,int &b)
{
	int c=a;
	a=b;
	b=c;
}
void SapXep(int n)
{
	ifstream read;
	ofstream write;
	int m=0,*p;
	p=new int [n];
	read.open("/home/lucky/workspace/file/src/mang1chieu.txt");
	while(!read.eof())
	{
		read>>*(p+m);
		m++;
	}
	read.close();
	for(int i=0;i<m-1;i++)
	{
		for(int j=i+1;j<m;j++)
		{
			if(*(p+i)>*(p+j))
			{
				HoanVi(*(p+i),*(p+j));
			}
		}
	}
	write.open("/home/lucky/workspace/file/src/sapxep.txt");
	for(int i=0;i<m;i++)
	{
		write<<*(p+i)<<"\t";
	}
	write.close();
}
void Nhap2Chieu(int **p,int n,int m)
{
	srand(unsigned(time(0)));
	for(int i=0;i<n;i++)
	{
		*(p+i)=new int [m];
		for(int j=0;j<m;j++)
		{
			*(*(p+i)+j)=rand()%100;
		}
	}
}
void Ghi2Chieu(int **p,int n,int m)
{
	ofstream write;
	write.open("/home/lucky/workspace/file/src/ghi2chieu.txt");
	write<<n<<"\t"<<m<<"\n";
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			write<<*(*(p+i)+j)<<"\t";
		}
		write<<"\n";
	}
	write.close();
}
int Tong2Chieu(int n,int m)
{
	int **p;
	p=new int* [n];
	int s=0;
	ifstream read;
	read.open("/home/lucky/workspace/file/src/ghi2chieu.txt");
	read>>n;
	read>>m;
	for(int i=0;i<n;i++)
	{
		*(p+i)=new int [m];
		for(int j=0;j<m;j++)
		{
			read>>*(*(p+i)+j);
		}
	}
	read.close();
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			s+=*(*(p+i)+j);
		}
	}
	return s;
}
void SapXep2Chieu(int n,int m)
{
	int **p,*q,k=0;
	p=new int*[n];
	q=new int [n*m];
	ofstream write;
	ifstream read;
	read.open("/home/lucky/workspace/file/src/ghi2chieu.txt");
	read>>n;
	read>>m;
	for(int i=0;i<n;i++)
	{
		*(p+i)=new int [m];
		for(int j=0;j<m;j++)
		{
			read>>*(*(p+i)+j);
		}
	}
	read.close();
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			*(q+k)=*(*(p+i)+j);
			k++;
		}
	}
	for(int i=0;i<k-1;i++)
	{
		for(int j=i+1;j<k;j++)
		{
			if(*(q+i)>*(q+j))
				HoanVi(*(q+i),*(q+j));
		}
	}
	k=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			*(*(p+i)+j)=*(q+k);
			k++;
		}
	}
	write.open("/home/lucky/workspace/file/src/sapxep2chieu.txt");
	write<<n<<"\t"<<m<<"\n";
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			write<<*(*(p+i)+j)<<"\t";
		}
		write<<"\n";
	}
	write.close();
}
